package com.shmn.gongjiajumain.controller;

import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.shmn.gongjiajumain.vo.CodeVo;
import com.shmn.gongjiajumain.vo.ScrapSubmitHistory.ScrapSubmitHistoryFormVo;
import com.shmn.gongjiajumain.vo.ScrapSubmitHistory.ScrapSubmitHistoryTableVo;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.shmn.gongjiajumain.entity.ScrapSubmitHistoryEntity;
import com.shmn.gongjiajumain.service.ScrapSubmitHistoryService;
import com.shmn.common.utils.PageUtils;
import com.shmn.common.utils.R;


/**
 * 记录历史报废申请
 *
 * @author chenshun
 * @email 1005074582@qq.com
 * @date 2023-05-14 16:40:27
 */
@RestController
@RequestMapping("gongjiajumain/scrapsubmithistory")
public class ScrapSubmitHistoryController {
    @Autowired
    private ScrapSubmitHistoryService scrapSubmitHistoryService;

    @PostMapping("getformdata")
    public void getFormData(@RequestBody ScrapSubmitHistoryFormVo scrapSubmitHistoryFormVo){
        System.out.println(scrapSubmitHistoryFormVo);
        ScrapSubmitHistoryEntity scrapSubmitHistoryEntity = new ScrapSubmitHistoryEntity();
        BeanUtils.copyProperties(scrapSubmitHistoryFormVo,scrapSubmitHistoryEntity);
        System.out.println(scrapSubmitHistoryEntity);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("gongjiajumain:scrapsubmithistory:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = scrapSubmitHistoryService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/list1")
    //@RequiresPermissions("gongjiajumain:scrapsubmithistory:list")
    public R list1(@RequestBody ScrapSubmitHistoryEntity scrapSubmitHistory){
        PageUtils page = scrapSubmitHistoryService.queryPage1(scrapSubmitHistory);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("gongjiajumain:scrapsubmithistory:info")
    public R info(@PathVariable("id") Integer id){
		ScrapSubmitHistoryEntity scrapSubmitHistory = scrapSubmitHistoryService.getById(id);

        return R.ok().put("scrapSubmitHistory", scrapSubmitHistory);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("gongjiajumain:scrapsubmithistory:save")
    public R save(@RequestBody ScrapSubmitHistoryEntity scrapSubmitHistory){
		scrapSubmitHistoryService.save(scrapSubmitHistory);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")

    //@RequiresPermissions("gongjiajumain:scrapsubmithistory:update")
    public R update(@RequestBody ScrapSubmitHistoryEntity scrapSubmitHistory){

		scrapSubmitHistoryService.updateById(scrapSubmitHistory);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("gongjiajumain:scrapsubmithistory:delete")
    public R delete(@RequestBody Integer[] ids){
		scrapSubmitHistoryService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @PostMapping("/exportExcel")
    public ResponseEntity<byte[]> exportExcel(@RequestBody List<ScrapSubmitHistoryTableVo> list) {
        try (Workbook wb = new XSSFWorkbook()) {
            Sheet sheet = wb.createSheet("Sheet1");
            Row headerRow = sheet.createRow(0);
            headerRow.createCell(0).setCellValue("姓名");
            headerRow.createCell(1).setCellValue("年龄");
            headerRow.createCell(2).setCellValue("地址");

            int rowNum = 1;
            for (ScrapSubmitHistoryTableVo model : list) {
                Row row = sheet.createRow(rowNum++);
                row.createCell(0).setCellValue(model.getCode());
                row.createCell(1).setCellValue(model.getSeqId());
                row.createCell(2).setCellValue(model.getScrapReason());
                row.createCell(3).setCellValue(model.getSubmitTime());
                row.createCell(4).setCellValue(model.getStatus());
            }

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            wb.write(baos);
            byte[] data = baos.toByteArray();
            HttpHeaders headers = new HttpHeaders();
            headers.setContentDispositionFormData("attachment", "exported_data.xlsx");
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            return new ResponseEntity<>(data, headers, HttpStatus.OK);
        } catch (Exception e) {
            throw new RuntimeException("导出Excel失败！");
        }
    }

}
